package ui.client;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.KeyListener;
import com.gwtext.client.widgets.event.PanelListenerAdapter;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;

/**
 * the class of ChatWindow stands for 
 * a chat window with a friend
 * @author wangkaidi
 *
 */
public class ChatWindow extends Window
{
	private GroupEntry entry;
	private Client main;//the reference to the main 
	private TextArea messages=new TextArea();
	private TextArea send=new TextArea();
	
	public ChatWindow(Client main,GroupEntry entry)
	{
		this.main=main;
		this.entry=entry;
		init();
	}
	
	/**
	 * init() will setup the components
	 */
	private void init()
	{		
		try
		{		
			messages.setReadOnly(true);			
			this.setTitle("chatting with "+entry.getAlias());
			this.setLayout(new RowLayout());
			this.add(messages,new RowLayoutData(220));		
			this.add(send,new RowLayoutData(100));	
					
			send.addKeyListener(EventObject.ENTER,new KeyListener()
			{
				public void onKey(int key, EventObject e)
				{		
					if(e.getKey()==EventObject.CONTROL)//if press Ctrl+Enter
					{
						String text=messages.getText();
						text+="\n";
						messages.setValue(text);
						messages.selectText(messages.getText().length(), messages.getText().length());
					}
					else//if press Enter
					{
						send();				
					}
				}				
			});
			
			send.addKeyListener(EventObject.CONTROL,new KeyListener()
			{
				public void onKey(int key, EventObject e)
				{	
					String text=send.getText();
					text+="\n";
					send.setValue(text);
					send.selectText(send.getText().length(), send.getText().length());
					
				}				
			});
			
			
			this.setWidth(300);
			this.setHeight(350);
			this.center();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		this.addListener(new PanelListenerAdapter()
		{
			public void onClose(Panel panel)//remove the ChatWindow with the GroupEntry
			{
				ChatWindow.this.setVisible(false);
				main.removeChatWindow(entry);
			}
		});
	}
	/**
	 * send the message to 
	 * the dest
	 */
	private void send()
	{
		String text=send.getText();
		if(text.trim().equals(""))
		{
			MessageBox.alert("please input the message");
			return;
		}
		else
		{
			main.send(text, entry);
			String content=messages.getText();
			content+=main.username+":\n";
			content+="    "+text+"\n";
			messages.setValue(content);
			messages.selectText(messages.getText().length(), messages.getText().length());			
			send.setValue("");//clear the input			
		}	
	}
	
	/**
	 * append a new instant message
	 * to the textarea
	 * @param im
	 */
	public void append(InstantMessage im)
	{
		if(im==null)return;
		System.out.println("append message");
		String content=messages.getText();
		content+=im.getFromID()+":\n";
		content+="    "+im.getContent()+"\n";	
		
		messages.setValue(content);
		messages.selectText(messages.getText().length(), messages.getText().length());		
	}
}
